package com.niuwa.p2p.service.impl.borrowinfomg.enterpriseinfomg;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.niuwa.p2p.core.mvc.entity.PageBean;
import com.niuwa.p2p.core.utils.DictConstant.ChannelAuditStatus;
import com.niuwa.p2p.core.utils.DictConstant.ChlAuditAction;
import com.niuwa.p2p.core.utils.DictConstant.FlagStatus;
import com.niuwa.p2p.dao.borrowinfomg.enterpriseinfomg.IEnterpriseinfoMgDao;
import com.niuwa.p2p.dao.channel.CorpAuditLogDao;
import com.niuwa.p2p.dao.channel.CorporationDao;
import com.niuwa.p2p.dao.channel.LoanClaimAuditDao;
import com.niuwa.p2p.entity.PageQuery;
import com.niuwa.p2p.entity.channel.CorpAuditLog;
import com.niuwa.p2p.entity.channel.Corporation;
import com.niuwa.p2p.entity.enterprise.CorpBorrowHistoryVO;
import com.niuwa.p2p.entity.enterprise.EnterpriseBlackVO;
import com.niuwa.p2p.exception.BusinessException;
import com.niuwa.p2p.service.borrowinfomg.enterpriseinfomg.ICorpLoginMgService;
import com.niuwa.p2p.service.borrowinfomg.enterpriseinfomg.IEnterpriseinfoMgService;
import com.niuwa.p2p.vo.borrowinfomg.enterpriseinfomg.BorrowCorpVo;
import com.niuwa.p2p.vo.borrowinfomg.enterpriseinfomg.CorpBasicInfoVo;
import com.niuwa.p2p.vo.channel.AuditFrom;

@Service
public class EnterpriseinfoMgServiceImpl implements IEnterpriseinfoMgService {
	@Autowired
	private IEnterpriseinfoMgDao enterpriseinfoMgDao;
	
	@Autowired
	private CorporationDao corporationDao;
	
	@Autowired
	private CorpAuditLogDao corpAuditLogDao;
	
	@Autowired
	private LoanClaimAuditDao loanClaimAuditDao;
	
	@Autowired
	private ICorpLoginMgService corpLoginMgService;
	/**
	 * 分页查询借款企业集合
	 * 根据paramMap，选取需要的渠道客户集合 
	 * 
	 * @param paramMap 过滤得条件Map
	 * @return PageBean<BorrowCorpVo> 借款企业 分页信息集合
	 */
	@Override
	public PageBean<BorrowCorpVo> search(Map<String, Object> paramMap) {

		assert paramMap.get(PageQuery.PAGEQUERY) != null;

		int totalCount = enterpriseinfoMgDao.count(paramMap);

		PageBean<BorrowCorpVo> page = new PageBean<BorrowCorpVo>(totalCount);

		// 设置当前页
		page.setPage(((PageQuery) paramMap.get(PageQuery.PAGEQUERY)).getPage());

		page.setRows(enterpriseinfoMgDao.selectBorrowCorpBySelective(paramMap));

		return page;
	}

	/**
	 * 删除
	 * @param corpCode 企业编号
	 * @param operatorId 操作员ID 
	 * @throws Exception
	 */
	public int deleteBorrowCopr(String corpCode, long operatorId) throws Exception{
		int result = 0;
		// 取得企业信息
		Corporation corporation = corporationDao.selectByPrimaryKey(corpCode);
		// 不存在,抛出错误
		if (corporation == null) {
			throw new BusinessException("EnterpriseinfoMgService.corporationIsNotExist");
		}
		// 设置:delFlg = y
		corporation.setDelFlag(FlagStatus.Y.name());
		// 更新企业信息
		result = corporationDao.updateByPrimaryKeySelective(corporation);
		if(result != 1) {
			throw new BusinessException("EnterpriseinfoMgService.failTOToDelCorporation");
		}
		return result;
	}
		
	/**
	 * 提交审核
	 * @param corpCode 企业编号
	 * @param operatorId 操作员ID 
	 * @throws Exception
	 */
	public int updateToAuditBorrowCopr(String corpCode, long operatorId) throws Exception{
		int result = 0;
		// 取得企业信息
		Corporation corporation = corporationDao.selectByPrimaryKey(corpCode);
		// 不存在,抛出错误
		if (corporation == null) {
			throw new BusinessException("EnterpriseinfoMgService.corporationIsNotExist");
		}
		// 设置审核状态:审核中
		corporation.setAuditStatus(ChannelAuditStatus.AUDITING.getCode());
		// 更新渠道信息
		result = corporationDao.updateByPrimaryKeySelective(corporation);
		if(result != 1) {
			throw new BusinessException("EnterpriseinfoMgService.failTOEnableCorporation");
		}
		return result;
	}
	
	/**
	 * 审核
	 * @param corpCode 企业编号
	 * @param auditFrom 审核Form 
	 * @param operatorId 操作员ID
	 * @throws Exception
	 */
	public int updateAuditBorrowCopr(String corpCode, AuditFrom auditFrom, long operatorId) throws Exception{
		int result = 0;
		// 取得企业信息
		Corporation corporation = corporationDao.selectByPrimaryKey(corpCode);
		// 不存在,抛出错误
		if (corporation == null) {
			throw new BusinessException("EnterpriseinfoMgService.corporationIsNotExist");
		}		
		// 设置渠道审核类型
		if(ChlAuditAction.PASS.getCode().equals(auditFrom.getOperateType())) {
			corporation.setAuditStatus(ChannelAuditStatus.PASS.getCode());
		}else if(ChlAuditAction.RETURN.getCode().equals(auditFrom.getOperateType())) {
			corporation.setAuditStatus(ChannelAuditStatus.RETURN.getCode());
		}else if(ChlAuditAction.REJECT.getCode().equals(auditFrom.getOperateType())) {
			corporation.setAuditStatus(ChannelAuditStatus.REJECT.getCode());
		}
			
		// 更新企业信息
		result = corporationDao.updateByPrimaryKeySelective(corporation);
		if(result != 1) {
			throw new BusinessException("EnterpriseinfoMgService.failTOAuditcorporation");
		}
		// 添加渠道审核操作记录
		CorpAuditLog corpAuditLog = new CorpAuditLog();
		corpAuditLog.setCorpCode(corpCode);
		// 设定渠道审核类型
		corpAuditLog.setOperateType(auditFrom.getOperateType());
		corpAuditLog.setCreateTime(new Date());
		corpAuditLog.setOperator(operatorId);
		// 设定渠道审核理由
		corpAuditLog.setRemark(auditFrom.getRemark());
		result = corpAuditLogDao.insert(corpAuditLog);
		if(result != 1) {
			throw new BusinessException("EnterpriseinfoMgService.failTOAddCorporationAuditLog");
		}
		//开通联系人账号并发送邮件通知
		corpLoginMgService.saveCorpContactor(corpCode);
		return result;
	}
	
	/**
	 * 启用
	 * @param corpCode 企业编号
	 * @param operatorId 操作员ID
	 * @throws Exception
	 */
	public int updateEnableBorrowCorp(String corpCode, long operatorId) throws Exception{
		int result = 0;
		// 取得企业信息
		Corporation corporation = corporationDao.selectByPrimaryKey(corpCode);
		// 不存在,抛出错误
		if (corporation == null) {
			throw new BusinessException("EnterpriseinfoMgService.corporationIsNotExist");
		}
		// 设置审核状态:材料录入
		corporation.setAuditStatus(ChannelAuditStatus.EDIT.getCode());
		// 更新企业信息
		result = corporationDao.updateByPrimaryKeySelective(corporation);
		if(result != 1) {
			throw new BusinessException("EnterpriseinfoMgService.failTOEnableCorporation");
		}
		return result;
	}
	
	/**
	 * 取得企业基本信息
	 * @param corpCode 企业编号
	 * @return ChannelBasicInfoVo 渠道基本信息
	 */
	public CorpBasicInfoVo getCorpBasicInfo(String corpCode){
		return enterpriseinfoMgDao.selectCorpBasicInfoByChannelCode(corpCode);	
	}
	

	/**
	 * 添加黑名单企业
	 */
	@Override
	public void addNewBlackEnterprise(EnterpriseBlackVO enterpriseBlackVO) {
		enterpriseinfoMgDao.addBlackList(enterpriseBlackVO);
	}
	
	public PageBean<CorpBorrowHistoryVO> getBorrowHistorys(Map<String,Object> paramMap){
		assert paramMap.get(PageQuery.PAGEQUERY) != null;
		List<CorpBorrowHistoryVO> enterpriseBorrowHistorylist =  enterpriseinfoMgDao.selectBorrowHistory(paramMap);
		int totalCount = enterpriseinfoMgDao.countBorrowHistory(paramMap);
		PageBean<CorpBorrowHistoryVO> page = new PageBean<CorpBorrowHistoryVO>(totalCount);
		// 设置当前页
		page.setPage(((PageQuery) paramMap.get(PageQuery.PAGEQUERY)).getPage());
		page.setRows(enterpriseBorrowHistorylist);
		return page;
	}

}
